
package com.xiaoma.myaudience.biz.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.app.AudienceApplication;
import com.xiaoma.myaudience.biz.task.DataAsyncTask;
import com.xiaoma.myaudience.biz.task.ImageLoadingTask;
import com.xiaoma.myaudience.util.Pair;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask.DataAsyncCallback;

/**
 * 图片大小，默认图片，切图 ？ <br/>
 * 异步加载图片的ImageView. <br/>
 * 当有如下情况时使用: WeakReference保存的图片对象可能被回收，需要重新加载 <br/>
 * 设置图片前必须先调用{@link #cancelLoadingTask} <br/>
 * 
 */
public class LoadingImageView extends ImageView implements DataAsyncCallback {
    private static final String TAG = "LoadingImageView";

    private static final int RETRY_NUM = 3;
    private ImageLoadingTask mImageLoadingTask;

    private int mDefalutIcon = R.drawable.revice_user_head;//默认使用该图像

    private int mRetryNum;

    public LoadingImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LoadingImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoadingImageView(Context context) {
        super(context);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        cancelLoadingTask();
    }

    /**
     * 设置Loading时的图片
     * 
     * @param icon
     */
    public void setLoadingIcon(int icon) {
        mDefalutIcon = icon;
    }

    /**
     * 设置图片.
     * 
     * @param path 可以是URL地址，也可以是本地文件
     */
    public void setLoadingDrawable(String path) {
        setLoadingDrawable(path, PicUtils.composeSize(getWidth(), getHeight()));
    }

    /**
     * 设置图片.
     * 
     * @param path 可以是URL地址，也可以是本地文件
     * @param size 保存图片大小
     */
    public void setLoadingDrawable(String path, String size) {
        // mRetryNum = RETRY_NUM;
        mRetryNum = 0;
        cancelLoadingTask();

        AudienceApplication app = (AudienceApplication) (getContext().getApplicationContext());
        Bitmap bitmap = app.getBitmapCache(path);
        if (bitmap != null) {
            setImageBitmap(bitmap);
        } else {
            startLoadingTask(path, size);
        }
    }
    
    public void setHeadLoadingDrawable(String path, String size) {
    	// mRetryNum = RETRY_NUM;
    	mRetryNum = 0;
    	cancelLoadingTask();
    	
    	AudienceApplication app = (AudienceApplication) (getContext().getApplicationContext());
    	Bitmap bitmap = app.getBitmapCache(path);
    	if (bitmap != null) {
    		setImageBitmap(bitmap);
    	} else {
    		startHeadLoadingTask(path, size);
    	}
    }
    
    /**
     * 启动加载任务
     * @param path
     * @param size
     */
    private void startLoadingTask(String path, String size) {
        setImageResource(mDefalutIcon);
        mImageLoadingTask = new ImageLoadingTask(getContext(), DataAsyncTask.TASK_IMAGE_LORDING,
                this);
        mImageLoadingTask.execute(path, size);
    }
    
    private void startHeadLoadingTask(String path, String size) {
    	setImageResource(mDefalutIcon);
    	mImageLoadingTask = new ImageLoadingTask(getContext(), DataAsyncTask.TASK_IMAGE_LORDING,
    			this, true);
    	mImageLoadingTask.execute(path, size);
    }

    /**
     * 取消当前正在进行的加载图片任务. 在设置图片前一定要调用，否则可以出现图片变化
     */
    public void cancelLoadingTask() {
        if (mImageLoadingTask != null) {
            mImageLoadingTask.cancel(true, true);
        }
        mImageLoadingTask = null;
    }

    /**
     * 内部使用，用于加载图片完成后调用，同时调用LoadingDrawableCallback回调.
     * 
     * @param path
     * @param d
     */
    private void setLoadingDrawableInternal(String path, String size, Bitmap d) {
        cancelLoadingTask();
        if (d != null) {
            AudienceApplication app = (AudienceApplication) (getContext().getApplicationContext());
            app.addBitmapCache(path, d);
            setImageBitmap(d);            
        } else if ((++mRetryNum) < RETRY_NUM) {
            startLoadingTask(path, size);
        }
    }

    @Override
    public void onPostExecute(int what, Object o) {
        switch (what) {
            case DataAsyncTask.TASK_IMAGE_LORDING:
                Pair<String[], Bitmap> ret = (Pair<String[], Bitmap>) o;
                if (ret != null) {
                    setLoadingDrawableInternal(ret.first[0], ret.first[1], ret.second);
                }

                break;

            default:
                break;
        }

    }

    @Override
    public void onCancelled(int what) {

    }

    @Override
    public void onPreExecute(int what) {

    }

    @Override
    public void onProgressUpdate(int what, Object o) {

    }
}
